package com.fire.control.system.module.duty.controller.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.control.system.entity.Duty;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.duty.dao.DutyMapper;
import com.fire.control.system.module.duty.dao.DutyRepository;
import com.fire.control.system.module.duty.service.IDutyService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.duty.DutyRequestProtocol;
import com.fire.control.system.support.protocol.duty.LeaveDutyRequestProtocol;

/**
 * @author liuxincheng
 * @ClassName: WebDutyControl
 * @Description: TODO(值班管理控制器--Web端)
 * @date 2017年6月22日 下午5:09:41
 */
@RestController
@RequestMapping("/web/webDutyCtr")
public class WebDutyControl extends BaseController implements BusinessInterfaceStandard<DutyRequestProtocol, ResponseProtocol> {

	/**
	 * 值班服务注入
	 */
	@Autowired
	private IDutyService dutyService;
	@Autowired
	private DutyRepository dutyRepository;
	@Autowired
	private ProtocolSelector protocolSelector;
	@Autowired
	private DutyMapper dutyMapper;

	/**
	 * @author liuxincheng
	 * @Title: getDutySheetByCondition
	 * @Description: TODO(跳到值班列表页面)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/jumpToDutySheetPage", method = RequestMethod.POST)
	public ResponseProtocol jumpToDutySheetPage(DutyRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.jumpToDutySheetPage(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getDutySheet
	 * @Description: TODO(根据条件获取值班列表)
	 * @param @param
	 *            requestprotocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/getDutySheetByCondition", method = RequestMethod.POST)
	public ResponseProtocol getDutySheetByCondition(LeaveDutyRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.getDutySheetByCondition(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: jumpToUpdatePage
	 * @Description: TODO(跳到修改/添加值班信息页面)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/jumpToUpdatePage", method = RequestMethod.POST)
	public ResponseProtocol jumpToUpdatePage(DutyRequestProtocol requestProtocol, HttpServletRequest request) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.jumpToUpdatePage(requestProtocol,this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: jumpToUpdatePage
	 * @Description: TODO(获取值班日期)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/getDutyInfo", method = RequestMethod.POST)
	public ResponseProtocol getDutyInfo(DutyRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.addDutyInfo(requestProtocol);
		return response;
	}

	@RequestMapping(path = "/editDutyInfoByUserIdAnddutyMonth", method = RequestMethod.POST)
	public ResponseProtocol editDutyInfoByUserIdAnddutyMonth(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.editDutyInfoByUserIdAnddutyMonth(leaveDutyRequestProtocol);
	}

	/**
	 * @Title: updateDutyInfo
	 * @Description: TODO(修改值班信息)
	 * @param @param
	 *            leaveDutyRequestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/updateDutyInfo", method = RequestMethod.POST)
	public ResponseProtocol updateDutyInfo(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.addOrUpdateDutyInfo(leaveDutyRequestProtocol);
	}

	/**
	 * @Title: addDutyInfo
	 * @Description: TODO(添加值班信息)
	 * @param @param
	 *            leaveDutyRequestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/addDutyInfo", method = RequestMethod.POST)
	public ResponseProtocol addDutyInfo(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(leaveDutyRequestProtocol);
		try {
			List<Duty> dutys = leaveDutyRequestProtocol.getDutys();
			// 获取userId
			String userId = dutys.get(0).getUserId();
			String dutyMonth = leaveDutyRequestProtocol.getDutyMonth();
			String startTime = dutyMonth + "-01 00:00:00";
			String endTime = dutyMonth + "-31 23:59:59";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			leaveDutyRequestProtocol.setUserId(userId);
			leaveDutyRequestProtocol.setStartTime(sdf.parse(startTime).toString());
			leaveDutyRequestProtocol.setEndTime(sdf.parse(endTime).toString());
			
			// 查找已存在班次
			List<Duty> listDutys = dutyMapper.getDutySheetByCondition(leaveDutyRequestProtocol);
			if (listDutys != null || !listDutys.isEmpty()) {
				dutyRepository.delete(listDutys);
			}
			return dutyService.addOrUpdateDutyInfo(leaveDutyRequestProtocol);
		} catch (ParseException e) {
			e.printStackTrace();
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("值班信息添加失败!");
			return response;
		}

	}

	@RequestMapping(path = "/getDutyEmptyByUserId", method = RequestMethod.POST)
	public ResponseProtocol getDutyEmptyByUserId(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.getDutyEmptyByUserId(leaveDutyRequestProtocol);
	}

	@RequestMapping(path = "/findDutyList", method = RequestMethod.POST)
	public ResponseProtocol findDutyList(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.findDutyList(leaveDutyRequestProtocol);
	}

	/**
	 * @author liuxincheng
	 * @Title: updateDutyInfomation
	 * @Description: TODO(修改值班信息)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/updateDutyInfomation", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol updateDutyInfomation(DutyRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.updateDutyInfomation(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: deleteDutyInfomation
	 * @Description: TODO(删除值班信息)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/deleteDutyInfomation", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol deleteDutyInfomation(DutyRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = dutyService.deleteDutyInfomation(requestProtocol);
		return response;
	}

	/**
	 * @author zhanhongtan
	 * @Title: updateDutyLeaveStatusById
	 * @Description: TODO(修改请假状态)
	 * @param @param
	 *            leaveDutyRequestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/updateDutyLeaveStatusById", method = RequestMethod.POST)
	public ResponseProtocol updateDutyLeaveStatusById(LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.updateDutyLeaveStatus(leaveDutyRequestProtocol);
	}

	/**
	 * @author zhanhogntan @Title: updateDutyLeaveStatus @Description:
	 *         TODO(批量审批请假) @param @param
	 *         leaveDutyRequestProtocol @param @return 设定文件 @return
	 *         ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "/updateDutyLeaveStatusByIds", method = RequestMethod.POST)
	public ResponseProtocol updateDutyLeaveStatusByIds(LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.updateDutyLeaveStatusByIds(leaveDutyRequestProtocol);
	}

	/**
	 * @author zhanhogntan @Title: findDutyList @Description:
	 *         TODO(请假列表) @param @param leaveDutyRequestProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	// @RequestMapping(path="/findDutyList",method=RequestMethod.POST)
	@PostMapping(value = "/findDutyLeaveList", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseProtocol findDutyLeaveList(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.findDutyLeaveList(leaveDutyRequestProtocol);
	}

	/**
	 * @author zhanhogntan @Title: findSignList @Description:
	 *         TODO(补签到列表) @param @param leaveDutyRequestProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "/findSignList", method = RequestMethod.POST)
	public ResponseProtocol findSignList(@RequestBody LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.findSignList(leaveDutyRequestProtocol);
	}

	/**
	 * @author zhanhogntan @Title: updateSignStatusById @Description:
	 *         TODO(补签到审批) @param @param leaveDutyRequestProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "/updateSignStatusById", method = RequestMethod.POST)
	public ResponseProtocol updateSignStatusById(LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.updateSignStatusById(leaveDutyRequestProtocol);
	}

	/**
	 * @author zhanhogntan @Title: updateSignStatusByIds @Description:
	 *         TODO(补签到批量审批) @param @param
	 *         leaveDutyRequestProtocol @param @return 设定文件 @return
	 *         ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "/updateSignStatusByIds", method = RequestMethod.POST)
	public ResponseProtocol updateSignStatusByIds(LeaveDutyRequestProtocol leaveDutyRequestProtocol) {
		leaveDutyRequestProtocol.setTaskuuid(UUID.randomUUID().toString());
		return dutyService.updateSignStatusByIds(leaveDutyRequestProtocol);
	}

}
